﻿Ext.define('GS.view.event.Index', {
    extend: 'Ext.Container',
    xtype: 'eventindex',
    id: 'eventindex',

    config: {

        title: 'Events',
        iconCls: 'time',

        layout: {
            type: 'vbox'
        },

        items: [
        {
            xtype: 'toolbar',
            title: 'Events',
            docked: 'top',
        },
        {
            xtype: 'list',
            flex: 1,
            loadingText: "Loading Events...",
            emptyText: "<p>Der er i øjeblikket ingen events.</p>",
            grouped: true,
            itemTpl: '<b>{title}</b><div style="float: right;">{date:date("d/m/Y - H:i")}</div><br/>Sted: {location}',
            id: 'eventlist',

            store: {
                model: 'GS.model.event.Event',

                fields: [
                    'title', 'description', 'location', 'date'
                ],

                proxy: {
                    type: 'jsonp',
                    url: GS.util.Config.getServerUrl()+'GetEvents.php',
                    reader: {
                        type: 'json',
                        rootProperty: 'items'
                    }
                },

                sorters: [{ property: 'date', direction: 'ASC'}],

                grouper: {
                    sortProperty: "date",
                    direction: "ASC",
                    groupFn: function (record) {
                        if (record && record.data.date) {
                            return record.data.date.toDateString();
                        } else {
                            return '';
                        }
                    }
                },

                autoLoad: false,
                
                listeners: {
                    beforeload: function (store, operation) {
                        var user = Ext.create('GS.store.UserData');

                        user.load();

                        var school_id = user.getAt(0).get('school_id');
                        var url = GS.util.Config.getServerUrl()+'GetEvents.php?school_id=' + school_id;
                        store.getProxy().setUrl(url);
                    }
                }
            }
        },
        {
            xtype: 'button',
            text: 'Tilføj Event',
            ui: 'confirm',
            id: 'btnAddEvent',
            docked: 'bottom',
            height: '30px',
            margin: 10
        }],

        listeners: {
            show: function(list, opts){
                Ext.getCmp('eventlist').getStore().load();
            }
        }
    }
});

